#include <QTextCodec>
#include <QtDebug>
#include "playlist.h"

extern "C" {
#include "sandcat.h"
}

Playlist::Playlist(quint32 &myOffset, QObject *parent) :
    QAbstractTableModel(parent)
{
    this->myOffset = myOffset;

    this->insertColumns(0, 5);
    this->setHeaderData(3, Qt::Horizontal, tr("title"));
    this->setHeaderData(4, Qt::Horizontal, tr("artist"));
}
int Playlist::rowCount( const QModelIndex & parent  )  const
{
    int total_entry = LIST_total_entries_number(myOffset);
    return total_entry;
}
int Playlist::columnCount( const QModelIndex & parent )  const
{
    return 5;
}
QVariant Playlist::data ( const QModelIndex & index, int role )  const
{
    if( ! index.isValid()) return QVariant();

    if (role == Qt::TextAlignmentRole)
    {
        if(index.column() == 4) return Qt::AlignRight;
    }
    else if (role == Qt::DisplayRole)
    {
        if(index.column() == 0)
        {
            return QVariant(QString::number(myOffset));
        }
        else if(index.column() == 1)
        {
            return QVariant(QString::number(index.row()));
        }
        else if(index.column() == 2)
        {
            return QVariant(QString::number(LIST_sequence(myOffset, index.row())));
        }
        else if(index.column() == 3)
        {
            char *title = LIST_entry_array(myOffset, index.row())->title;
	    return QVariant( tr(title) );
        }
        else if(index.column() == 4)
        {
            char *artist = LIST_entry_array(myOffset, index.row())->artist;
	    return QVariant( tr(artist) );
        }
    }
    return QVariant();
}
